using Admin2025.Application.DTOs;
using Admin2025.Application.IServices;
using Admin2025.Domain.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Admin2025.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class UsersController : ControllerBase
{
  private readonly IUserService _userService;

  public UsersController(IUserService userService)
  {
    _userService = userService;
  }

  // 分页
  [HttpGet("paged")]
  public async Task<IActionResult> GetUsersPaged([FromQuery] PagedRequest request)
  {
    var result = await _userService.GetUsersPagedAsync(request);
    return Ok(result);
  }

  [HttpGet]
  // [Authorize(Roles = "Admin")]
  public async Task<IActionResult> GetAll()
  {
    var users = await _userService.GetAllUsersAsync();
    return Ok(users);
  }

  [HttpGet("{id}")]
  public async Task<IActionResult> GetById(Guid id)
  {
    var user = await _userService.GetUserByIdAsync(id);
    return user == null ? NotFound() : Ok(user);
  }

  [HttpPut("{id}")]
  public async Task<IActionResult> UpdateUser(Guid id, [FromBody] UpdateUserDto dto)
  {
    var success = await _userService.UpdateUserAsync(id, dto);
    return success ? Ok("User updated") : NotFound("User not found");
  }

  [HttpDelete("{id}")]
  public async Task<IActionResult> DeleteUser(Guid id)
  {
    var success = await _userService.DeleteUserAsync(id);
    return success ? Ok("User deleted") : NotFound("User not found");
  }
}